/**
 * 流程模型管理 API
 */
import type {
  ProcessDefinition,
  ProcessDefinitionQuery,
  ProcessModel,
} from './types';

import { requestClient } from '../request';

/**
 * 获取流程模型列表
 */
export async function getProcessModelListApi(params: ProcessDefinitionQuery) {
  return requestClient.get<{
    list: ProcessModel[];
    total: number;
    pageNum: number;
    pageSize: number;
  }>('/workflow/api/model/list', { params });
}

/**
 * 获取流程模型详情
 */
export async function getProcessModelDetailApi(id: string) {
  return requestClient.get<ProcessModel>(`/workflow/api/model/${id}`);
}

/**
 * 创建流程模型
 */
export async function createProcessModelApi(data: ProcessModel) {
  return requestClient.post<ProcessModel>('/workflow/api/model/create', data);
}

/**
 * 更新流程模型
 */
export async function updateProcessModelApi(data: ProcessModel) {
  return requestClient.put<ProcessModel>(
    `/workflow/api/model/${data.id}`,
    data,
  );
}

/**
 * 删除流程模型
 */
export async function deleteProcessModelApi(id: string) {
  return requestClient.delete(`/workflow/api/model/${id}`);
}

/**
 * 保存流程模型的BPMN XML
 */
export async function saveProcessModelBpmnApi(id: string, bpmnXml: string) {
  return requestClient.post(`/workflow/api/model/${id}/bpmn`, { bpmnXml });
}

/**
 * 获取流程模型的BPMN XML
 */
export async function getProcessModelBpmnApi(id: string) {
  return requestClient.get<{ bpmnXml: string }>(
    `/workflow/api/model/${id}/bpmn`,
  );
}

/**
 * 部署流程模型
 */
export async function deployProcessModelApi(id: string) {
  return requestClient.post<ProcessDefinition>(
    `/workflow/api/model/${id}/deploy`,
  );
}

/**
 * 导出流程模型（BPMN XML）
 */
export async function exportProcessModelApi(id: string) {
  return requestClient.get<Blob>(`/workflow/api/model/${id}/export`, {
    responseType: 'blob',
  });
}

/**
 * 导入流程模型（BPMN XML）
 */
export async function importProcessModelApi(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return requestClient.post<ProcessModel>('/workflow/api/model/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

